package com.platform.system.service.impl;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.platform.common.annotation.DataScope;
import com.platform.common.utils.DateUtils;
import com.platform.system.domain.ProCart;
import com.platform.system.service.IProProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.platform.system.mapper.ProOrderMapper;
import com.platform.system.domain.ProOrder;
import com.platform.system.service.IProOrderService;

/**
 * 订单Service业务层处理
 *
 * @author platform
 * @date 2025-02-23
 */
@Service
public class ProOrderServiceImpl implements IProOrderService {
    @Autowired
    private ProOrderMapper proOrderMapper;

    @Autowired
    IProProductOrderService productOrderService;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public ProOrder selectProOrderById(Long id) {
        return proOrderMapper.selectProOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param proOrder 订单
     * @return 订单
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<ProOrder> selectProOrderList(ProOrder proOrder) {
        List<ProOrder> proOrders = proOrderMapper.selectProOrderList(proOrder);
        for (ProOrder order : proOrders) {
            Long orderId = order.getId();
            List<ProCart> carts = productOrderService.findProductByOrderId(orderId);
            order.setCarts(JSONUtil.toJsonStr(carts));
        }
        return proOrders;
    }

    /**
     * 查询订单列表
     *
     * @param proOrder 订单
     * @return 订单
     */
    @Override
    public List<ProOrder> selectProOrderListAll(ProOrder proOrder) {
        List<ProOrder> proOrders = proOrderMapper.selectProOrderListAll(proOrder);
        for (ProOrder order : proOrders) {
            Long orderId = order.getId();
            List<ProCart> carts = productOrderService.findProductByOrderId(orderId);
            order.setCarts(JSONUtil.toJsonStr(carts));
        }
        return proOrders;
    }

    /**
     * 新增订单
     *
     * @param proOrder 订单
     * @return 结果
     */
    @Override
    public int insertProOrder(ProOrder proOrder) {
        proOrder.setCreateTime(DateUtils.getNowDate());
        return proOrderMapper.insertProOrder(proOrder);
    }

    /**
     * 修改订单
     *
     * @param proOrder 订单
     * @return 结果
     */
    @Override
    public int updateProOrder(ProOrder proOrder) {
        proOrder.setUpdateTime(DateUtils.getNowDate());
        return proOrderMapper.updateProOrder(proOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteProOrderByIds(Long[] ids) {
        return proOrderMapper.deleteProOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteProOrderById(Long id) {
        return proOrderMapper.deleteProOrderById(id);
    }
}
